package com.yanqu.road.server.logic.activity.condition.patronsfund;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.player.UserBeauty;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.patronsfund.PatronsFundModule;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.gameplayer.module.player.BeautyModule;
import com.yanqu.road.server.gameplayer.module.task.TaskModule;
import com.yanqu.road.server.logic.activity.BaseActivityData;
import com.yanqu.road.server.logic.activity.condition.BaseActivityCondition;
import com.yanqu.road.server.manger.config.BeautyMgr;
import com.yanqu.road.server.manger.config.PatronsMgr;

import java.math.BigInteger;

public class PatronsFundBeautyGlamourCondition extends BaseActivityCondition {

    public PatronsFundBeautyGlamourCondition(ActivityInfo activityInfo, ActivityConditionInfo activityConditionInfo, BaseActivityData baseActivityData) {
        super(activityInfo, activityConditionInfo, baseActivityData);
    }

    private void changeValue(BigInteger val) {
        setValue(val);
        //玩家信息日志
        player.getModule(PatronsFundModule.class).savePatronsFundUserData(this.activityInfo.getChildType(), this.activityInfo.getActivityId(), 0, val.longValue());
        //达成日志
        if (this.isCompleted(player)) {
            player.getModule(PatronsFundModule.class).savePatronsFundConditionCom(this.activityInfo.getChildType(), this.activityInfo.getActivityId(), this.getConditionType(), this.getConditionId());
        }
    }

    @Override
    protected void player_BoardLevelHandle(Object object) {
        UserBeauty userBeauty = (UserBeauty) object;
        //判断任务的门客是否跟美女有缘分关系
        int relationBeautyId = this.activityConditionInfo.getParamList().get(1).intValue();//门客关联的美女ID
        if (relationBeautyId == userBeauty.getBeautyId()) {
            //美女总才华
            long glamour = userBeauty.getTotalGlamour();
            changeValue(BigInteger.valueOf(glamour));
        }
    }

    public void initDefaultValue() {
        //门客关联的美女ID
        int relationBeautyId = this.activityConditionInfo.getParamList().get(1).intValue();
        //美女总才华
        UserBeauty userBeauty = player.getModule(BeautyModule.class).getUserBeauty(relationBeautyId);
        if (userBeauty != null) {
            long glamour = userBeauty.getTotalGlamour();
            changeValue(BigInteger.valueOf(glamour));
        }
    }

    public boolean isCompleted(GamePlayer player) {
        //门客id
        int patronsId = this.activityInfo.getChildType();
        //第一位是是否要購買過
        if(activityConditionInfo.getParamList().get(0).compareTo(BigInteger.ONE) == 0){
            //是否購買了
            if(player.getModule(PatronsFundModule.class).checkIsBuyPatronsFund(patronsId)){
                return getValue().compareTo(activityConditionInfo.getParamList().get(3)) >= 0;
            }
        }else if(activityConditionInfo.getParamList().get(0).compareTo(BigInteger.ZERO) == 0){
            return getValue().compareTo(activityConditionInfo.getParamList().get(3)) >= 0;
        }
        return false;
    }

    @Override
    public void addTrigger(GamePlayer player) {
        player.getModule(TaskModule.class).getGameEvent().addListener(eGamePlayerEventType.PatronsFundBeautyGlamour.getValue(), handle);
    }

    @Override
    public void removeTrigger(GamePlayer player) {
        player.getModule(TaskModule.class).getGameEvent().removeListener(eGamePlayerEventType.PatronsFundBeautyGlamour.getValue(), handle);
    }
}
